<?php


namespace app\common\model;


use think\Model;

class RegionModel extends Model
{
    protected $name = 'region';


    /**
     * 获取地区列表
     * @return array
     */
    public function getList(): array
    {
        try {
            $where = [
                'status' => '1',
            ];
            $field = ['id','name'];
            $order = [
                'weigh' => 'asc'
            ];
            $region_ids = model('app\common\model\CompanyPositionModel')->column('region_ids');
            $region_ids = implode(',',$region_ids);
            $where['id'] = ['in',$region_ids];
            $list = $this
                ->where($where)
                ->field($field)
                ->order($order)
                ->select();

            $oldData = ['A' => [],'B' => [],'C' => [],'D' => [],'E' => [],'F' => [],'G' => [],'H' => [],'I' => [],'J' => [],'K' => [],'L' => [],'M' => [],'N' => [],'O' => [],'P' => [],'Q' => [],'R' => [],'S' => [],'T' => [],'U' => [],'V' => [],'W' => [],'X' => [],'Y' => [],'Z' => [],'#' => []];
            $letter = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
            foreach ($list as $row){
                $row->latter = getFirstChar($row['name']);
                if(in_array($row->latter,$letter)){
                    $oldData[$row->latter][] = $row;
                }else{
                    $oldData['#'][] = $row;
                }
            }
            $data = [];
//            $data[] = [
//                'id' => '#',
//                'data' => arraySort($oldData['#'],'name',SORT_ASC)
//            ];
            for ($i=0; $i<count($letter); $i++){
                $data[] = [
                    'id' => $letter[$i],
                    'data' => $oldData[$letter[$i]]
                ];
            }

            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => $data
            ];
        }catch (\Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => []
            ];
        }
    }

}